<?php
class menuLib
{
	/**
	 * @var menuMod
	 */
	private $menuMod;
	
	function __construct()
	{
		$this->menuMod=new menuMod();
	}
	
	public function getMenuList($menuId=FALSE,$depth=6)
	{
		if($menuId && is_numeric($menuId))
		{
			$menuId=intval($menuId);
		}
		else 
		{
			$menuId=false;
		}
		return $this->getMenuListSub(array(),$menuId, $depth);
	}
	
	private function getMenuListSub($data,$menuId,$depth)
	{
		$list=$this->menuMod->getMenuList($menuId,$depth);
		foreach ($list as $k=>$v)
		{
			$data[]=$v;
			if($v['depth']<=$depth)
			{
				$data=$this->getMenuListSub($data,$v['menuId'],$depth);
			}
		}
		unset($list);
		return $data;
	}
	
	public function getMenuInfo($menuId)
	{
		if(is_numeric($menuId))
		{
			$menuId=intval($menuId);
			return $this->menuMod->getMenuInfo($menuId);
		}
		return false;
	}
	
	public function addMenu($mname,$cname,$menuType,$showSub=0,$parent=0,$depth=1)
	{
		$info=$this->getMenuInfo($parent);
		if($parent && $info==false)
		{
			throw new Exception('上级菜单ID不存在');
		}
		if(is_numeric($cname))
		{
			throw new Exception('别名不能是纯数字');
		}
		$data['showSub']=$showSub ?intval($showSub) :0;
		$data['parentId']=$parent ?intval($parent) :0;
		$data['depth']=$parent ? ($info['depth']+1) :0;
		$data['menuType']=$menuType==1 ?1 :2;
		$data['name']=$mname;
		$data['cname']=$cname;
		$data['menuSort']=1;
		return $this->menuMod->addMenu($data);
	}
}